/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The software in this package is published under the terms of the GPL v2.0
 * license, a copy of which has been included with this distribution in the
 * LICENSE-GPL.txt file.
 */

package org.cemantika.management.drools;

import org.cemantika.exception.KnowledgeSessionAlreadyCreatedException;
import org.cemantika.exception.LoadKnowledgeSessionException;
import org.cemantika.factory.BeanFactory;
import org.cemantika.factory.DaoFactory;
import org.cemantika.infrastructure.model.ContextUser;
import org.cemantika.management.drools.dao.DaoDroolsContextualManipulation;
import org.cemantika.util.CemantikaLogger;
import org.cemantika.util.SpringUtil;

/**
 * DroolsConfiguration represents the class that configure a {@link DroolsContextualManipulation}
 * related to a {@link ContextUser}.
 * 
 * @author andersonfellipe
 */
public class DroolsConfiguration {

    /**
     * Represents the DAO object to manipulate the {@link DroolsContextualManipulation}
     * persistence.
     */
    private static DaoDroolsContextualManipulation dao;

    /**
     * Represents the configuration of a {@link ContextUser} related to a unique
     * {@link DroolsContextualManipulation} that contains the knowledge session.
     * 
     * @param contextUser the object of a {@link ContextUser}.
     * @return an object of a {@link DroolsContextualManipulation} related to this contextUser.
     */
    @SuppressWarnings("restriction")
    public static DroolsContextualManipulation configureContextUser(ContextUser contextUser) {
        try {
            dao = DaoFactory.getInstance().getDaoDroolsContextualManipulation();
            DroolsContextualManipulation droolsBean = BeanFactory.getBeanDroolsContextualManipulation();
            DroolsContextualManipulation drools = dao.findByContextUser(contextUser);
            if (drools == null) {
                drools = droolsBean;
                drools.setContextUser(contextUser);
                Integer id = drools.newKnowledgeSession();
                drools.setId(id);
                dao.save(drools);
            } else {
                drools.setContextUser(contextUser);
                drools.setKnowledgeBase(droolsBean.getKnowledgeBase());
                drools.setEntityManagerFactory(droolsBean.getEntityManagerFactory());
                drools.setJpaTransactionManager(droolsBean.getJpaTransactionManager());
                drools.setEnvironment(droolsBean.getEnvironment());
                drools.loadKnowledgeSession(drools.getId());
            }
            return drools;
        } catch (KnowledgeSessionAlreadyCreatedException e) {
            CemantikaLogger.logger.error("** " + e.getMessage());
            e.printStackTrace();
        } catch (LoadKnowledgeSessionException e) {
            CemantikaLogger.logger.error("** " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * Represents the update of a persisted {@link DroolsContextualManipulation}. 
     * 
     * @param drools an object of a {@link DroolsContextualManipulation}.
     */
    @SuppressWarnings("restriction")
    public static void updateDroolsContextualManipulation(DroolsContextualManipulation drools) {
        dao = (DaoDroolsContextualManipulation) SpringUtil.getApplicationContext()
                    .getBean("daoDroolsContextualManipulation");
        dao.update(drools);
    }

}
